package com.gxc.authcore.system.util;

import java.util.List;

import org.apache.log4j.Logger;

import com.gxc.authcore.system.domain.Param;
import com.gxc.authcore.system.service.ParamService;
import com.gxc.dbcore.util.BeanMap;
import com.gxc.webcore.util.ContextUtil;
import com.gxc.webcore.util.SystemUtil;

public class ParamUtil {

	private static Logger logger = Logger.getLogger(ParamUtil.class);

	/**
	 * 通用参数缓存
	 */
	private static BeanMap cache = new BeanMap();

	public static String get(String key) {
		if (cache.size() == 0) {
			ParamUtil.reload();
		}
		return (String) cache.get(key);
	}

	public static void put(String key, String value) {
		cache.put(key, value);
	}

	/**
	 * 参数初始化
	 */
	public static void init() {
		ParamUtil.reload();
	}

	/**
	 * 重新加载参数
	 */
	@SuppressWarnings("unchecked")
	public static void reload() {
		logger.info("加载系统参数开始。");
		ParamService paramService = (ParamService) ContextUtil
				.getBean(ParamService.class);
		List<Param> list = paramService.list(null);
		cache.clear();
		for (Param item : list) {
			cache.put(item.getParam_key().trim(), item.getParam_value());
		}

		// 加载url_param
		cache.put("url_local", cache.get("url_" + SystemUtil.get("serverName")));

		logger.info("加载系统参数结束。");
	}
}
